#include "CCity.h"
using namespace Content;

CCity::CCity()
{

}

void CCity::buildCity(const int numAreas)
{
  if (numAreas > 0)
  {
    int i = 0;
    CArea* area = new CArea();
    area->setTileCoord(0,0);
    area->setId(i);
    cityGraph.addNode(area);
    std::queue<CArea*> areas;

    ++i;
    if (i < numAreas)
    {
      areas.push(area);
    }

    while (!areas.empty())
    {
      area = areas.front();
      areas.pop();
      if (i <numAreas)
      {
        if (!area->left)
        {
          CArea* newArea = new CArea();
          newArea->setTileCoord(area->getTileX()-1,area->getTileY());
          newArea->setId(i);
          cityGraph.addNode(newArea);
          cityGraph.addEdge(area->getId(),newArea->getId());
          area->left = newArea;
          newArea->right = area;
          areas.push(newArea);
          ++i;
        }
      }
      if (i <numAreas)
      {
        if (!area->right)
        {
          CArea* newArea = new CArea();
          newArea->setTileCoord(area->getTileX()+1,area->getTileY());
          newArea->setId(i);
          cityGraph.addNode(newArea);
          cityGraph.addEdge(area->getId(),newArea->getId());
          area->right = newArea;
          newArea->left = area;
          areas.push(newArea);
          ++i;
        }
      }
      if (i <numAreas)
      {
        if (!area->bottom)
        {
          CArea* newArea = new CArea();
          newArea->setTileCoord(area->getTileX(),area->getTileY()+1);
          newArea->setId(i);
          cityGraph.addNode(newArea);
          cityGraph.addEdge(area->getId(),newArea->getId());
          area->bottom = newArea;
          newArea->top = area;
          areas.push(newArea);
          ++i;
        }
      }
    }
  }
}

CArea* CCity::getArea(const int areaId)
{
  return cityGraph.getNodeById(areaId);
}
